package bairui.international.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lianjing.sh.project.common.service.Service_Number;
import com.sh_lianjing.state.StateTransition;

import bairui.international.model.domain.jpa.TB_Admin;
import bairui.international.model.domain.jpa.TB_ElectronicTicket;
import bairui.international.model.domain.jpa.TB_ElectronicTicketOrder;
import bairui.international.model.status.State_ElectronicTicketOrder;
import bairui.international.model.type.Type_ElectronicTicket;
import bairui.international.service.Service_ElectronicTicket;
import bairui.international.service.Service_ElectronicTicketOrder;
import bairui.international.service.Service_ElectronicTicketOrderVerifyRecord;

/**
 * 
 * 【(电子券单)TB_ElectronicTicketOrder】服务实现
 *
 * @generated
 * 
 * @see bairui.international.model.domain.jpa.TB_ElectronicTicketOrder 模型
 * @see Service_ElectronicTicketOrder 服务接口
 * @see BService_ElectronicTicketOrderImpl 服务实现基础
 *
 */
@Service
public class Service_ElectronicTicketOrderImpl extends BService_ElectronicTicketOrderImpl implements Service_ElectronicTicketOrder {

	@Autowired private Service_ElectronicTicketOrderVerifyRecord service_ElectronicTicketOrderVerifyRecord;
	@Autowired private Service_Number service_Number;
	@Autowired private Service_ElectronicTicket service_ElectronicTicket;
	private final String ElectronicTicketOrderNumber = "ElectronicTicketOrderNumber";
	
	@Override
	public TB_ElectronicTicketOrder createInstance(TB_ElectronicTicketOrder input) {
		return dao.save(new TB_ElectronicTicketOrder(
				new TB_ElectronicTicketOrder.MustProperties() {
					@Override public Type_ElectronicTicket getType() {
						return input.getType();
					}
					@Override public State_ElectronicTicketOrder getState() {
						return State_ElectronicTicketOrder.t_create().to();
					}
					@Override public Double getPrice() {
						return input.getPrice();
					}
					@Override public Boolean getOneuseFlag() {
						return input.getOneuseFlag();
					}
					@Override public String getNumber() {
						return service_Number.generateDateFormatNumber(ElectronicTicketOrderNumber);
					}
					@Override public String getName() {
						return input.getName();
					}
					@Override public Boolean getGiveFlag() {
						return input.getGiveFlag();
					}
					@Override public Date getExpiryDate() {
						return input.getExpiryDate();
					}
					@Override public Long getCount() {
						return input.getCount();
					}
					@Override public Boolean getAgainFlag() {
						return false;
					}
				}, 
				new TB_ElectronicTicketOrder.OptionalSingleProperties() {
					@Override public TB_Admin getPublishor() {
						return null;
					}
					@Override public String getComment() {
						return input.getComment();
					}
					@Override public Date getApplyDate() {
						return null;
					}
					@Override public Date getGenerateDate() {
						return null;
					}
				}
			));
	}

	@Override
	public TB_ElectronicTicketOrder vertifyPass(long id) throws Exception {
		TB_ElectronicTicketOrder order = findById(id);
		// 更新信息
		StateTransition<State_ElectronicTicketOrder> trans = order.new UpdateProperties() {
			StateTransition<State_ElectronicTicketOrder> trans;
			@Override public State_ElectronicTicketOrder getState(TB_ElectronicTicketOrder it) throws Exception {
				trans = it.getState().t_verifyPass_from_Verifing();
				return trans.to();
			};
		}.trans;
		// 记录
		service_ElectronicTicketOrderVerifyRecord.createPassRecord(order, trans.comment(), null);
		//
		return order;
	}

	@Override
	public TB_ElectronicTicketOrder vertifyNoPass(long id, String message) throws Exception {
		TB_ElectronicTicketOrder order = findById(id);
		// 更新信息
		StateTransition<State_ElectronicTicketOrder> trans = order.new UpdateProperties() {
			StateTransition<State_ElectronicTicketOrder> trans;
			@Override public State_ElectronicTicketOrder getState(TB_ElectronicTicketOrder it) throws Exception {
				trans = it.getState().t_verifyNoPass_from_Verifing();
				return trans.to();
			};
		}.trans;
		// 记录
		service_ElectronicTicketOrderVerifyRecord.createNoPassRecord(order, trans.comment(), message);
		//
		return order;
	}

	@Override
	public TB_ElectronicTicketOrder submit(long id) throws Exception {
		TB_ElectronicTicketOrder order = findById(id);
		// 更新信息
		StateTransition<State_ElectronicTicketOrder> trans = order.new UpdateProperties() {
			StateTransition<State_ElectronicTicketOrder> trans;
			@Override public State_ElectronicTicketOrder getState(TB_ElectronicTicketOrder it) throws Exception {
				trans = it.getState().t_submit_to_Verifing();
				return trans.to();
			};
			@Override public Date getApplyDate(TB_ElectronicTicketOrder it) throws Exception {
				return new Date();
			};
		}.trans;
		// 记录
		service_ElectronicTicketOrderVerifyRecord.createFlowRecord(order, trans.comment());
		//
		return order;
	}

	@Override
	public TB_ElectronicTicketOrder generateTicket(long id) throws Exception {
		TB_ElectronicTicketOrder order = findById(id);
		// 更新信息
		StateTransition<State_ElectronicTicketOrder> trans = order.new UpdateProperties() {
			StateTransition<State_ElectronicTicketOrder> trans;
			@Override public State_ElectronicTicketOrder getState(TB_ElectronicTicketOrder it) throws Exception {
				trans = it.getState().t_generate_from_Published();
				return trans.to();
			};
			@Override public Date getGenerateDate(TB_ElectronicTicketOrder it) throws Exception {
				return new Date();
			};
		}.trans;
		// 记录
		service_ElectronicTicketOrderVerifyRecord.createFlowRecord(order, trans.comment());
		//
		List<TB_ElectronicTicket> arr = new ArrayList<>();
		for(int i=0; i<order.getCount(); i++){
			arr.add(service_ElectronicTicket.newInstance(order));
		}
		order.setElectronicTickets(arr);
		// 
		return order;
	}

}
